package com.cslg.ac.demo.service.donate;

import com.cslg.ac.demo.constant.CardType;
import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.donate.DonateDAO;
import com.cslg.ac.demo.dao.donate.DonationPO;
import com.cslg.ac.demo.dao.user.UserDAO;
import com.cslg.ac.demo.dao.user.person.PersonInfoDAO;
import com.cslg.ac.demo.dao.user.po.UserPO;
import com.cslg.ac.demo.dao.user.po.person.PersonInfoPO;
import com.cslg.ac.demo.entity.donate.Donation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DonateService {
    @Autowired
    PersonInfoDAO personInfoDAO;
    @Autowired
    UserDAO userDAO;
    @Autowired
    DonateDAO donateDAO;


    public Donation getInfo(int id){
        DonationPO donationPO = donateDAO.selectById(id);
        if(donationPO == null){
            return null;
        }
        Donation donation = new Donation();
        BeanUtils.copyProperties(donationPO, donation);
        return donation;
    }

    public int donateCard(Donation donation){
        DonationPO donationPO = new DonationPO();
        BeanUtils.copyProperties(donation, donationPO);
        UserPO user = userDAO.SelectById(donationPO.getTeamId());
        if(user == null || user.getUserType() == 1){
            return ResponseCode.PARAM_ILLEGAL;
        }
        PersonInfoPO personInfoPO = personInfoDAO.selectById(donation.getUserId());
        if(personInfoPO == null){
            return ResponseCode.PARAM_ILLEGAL;
        }
        if( donationPO.getCardType() == CardType.LOVE_CARD){
            personInfoPO.setLoveValue(donationPO.getCardCount());
        }else{
            personInfoPO.setLoveValue(donationPO.getCardCount()*3);
        }
        donateDAO.insert(donationPO);
        personInfoDAO.updateAllById(personInfoPO);
        return ResponseCode.REQUEST_SUCCESSED;
    }
}
